package com.gen.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gen.entity.Course;
import com.gen.service.ICourseService;

@Controller
public class CourseController {
	@Autowired
	ICourseService courseService;
	
	
	public ICourseService getCourseService() {
		return courseService;
	}

	public void setCourseService(ICourseService courseService) {
		this.courseService = courseService;
	}

	@RequestMapping("/addCoursePage.do")
	public String addCoursePage() {
		return "addCourse";
	}
	
	@RequestMapping("/addCourse.do")
	public ModelAndView add(Course course) {
		ModelAndView modelAndView = new ModelAndView();
		courseService.addCourse(course);
		modelAndView.setViewName("redirect:selectAllCourseStudents.do");
		return modelAndView;
	}
	
	@RequestMapping("/deleteCourse.do")
	public ModelAndView delete(String courseid) {
		ModelAndView modelAndView = new ModelAndView();
		courseService.deleteCourse(courseid);
		modelAndView.setViewName("redirect:selectAllCourseStudents.do");
		return modelAndView;
	}
	
	@RequestMapping("/updateCourse.do")
	public ModelAndView update(Course course) {
		ModelAndView modelAndView = new ModelAndView();
		courseService.updateCourse(course);
		modelAndView.setViewName("redirect:selectAllCourseStudents.do");
		return modelAndView;
	}
	
	@RequestMapping("/selectAllCourse.do")
	public ModelAndView selectAll() {
		ModelAndView modelAndView = new ModelAndView();
		List<Course> all = courseService.selectAllCourse();
		modelAndView.addObject("all", all);
		modelAndView.setViewName("redirect:selectAllCourseStudents.do");
		return modelAndView;
	}
	
	@RequestMapping("/selectOneCourse.do")
	public ModelAndView selectOne(String courseid) {
		ModelAndView modelAndView = new ModelAndView();
		Course course = courseService.selectOneCourse(courseid);
		modelAndView.addObject("course", course);
		modelAndView.setViewName("detailCourse");
		return modelAndView;
	}
	
	@RequestMapping("/toUpdateCourse.do")
	public ModelAndView toUpdate(String courseid) {
		ModelAndView modelAndView = new ModelAndView();
		Course course = courseService.selectOneCourse(courseid);
		modelAndView.addObject("course", course);
		modelAndView.setViewName("updateCourse");
		return modelAndView;
	}
	
	@RequestMapping("/selectCourseByParam.do")
	public ModelAndView selectByParam(Integer departid) {
		ModelAndView modelAndView = new ModelAndView();
		Course course = new Course();
		course.setDepartid(departid);
		List<Course> all = courseService.selectCourseByParam(course);
		modelAndView.addObject("all", all);
		modelAndView.setViewName("listCourse");
		return modelAndView;
	}
}
